package com.maxsalad.pospal.service;

import com.maxsalad.common.util.JsonUtil;
import com.maxsalad.common.util.OkHttpUtil;
import com.maxsalad.pospal.entity.dish.list.DishEntity;
import com.maxsalad.pospal.entity.dish.list.PostBackParameterEntity;
import com.maxsalad.pospal.entity.dish.list.ResponseDishListEntity;
import com.maxsalad.pospal.entity.order.add.ResponseOrder;
import com.maxsalad.uc.dao.DishDao;
import com.maxsalad.uc.dao.ShopDao;
import com.maxsalad.uc.pojo.TDishEntity;
import com.maxsalad.uc.pojo.TShopEntity;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by 2014-400 on 2016-03-16.
 */
@Service(value = "pospalDishService")
public class DishService extends BaseService {
    private static final Logger log = LoggerFactory.getLogger(DishService.class);

    @Autowired
    private DishDao dishDao;

    public ResponseDishListEntity getAllDish(long shopId) throws Exception {
        ResponseDishListEntity responseDishListEntity = getAllDish(shopId, "", "");
        if ("success".equals(responseDishListEntity.getStatus()) &&
                StringUtils.isNotBlank(responseDishListEntity.getData().getPostBackParameter().getParameterType()) &&
                StringUtils.isNotBlank(responseDishListEntity.getData().getPostBackParameter().getParameterValue())) {
            String parameterType = responseDishListEntity.getData().getPostBackParameter().getParameterType();
            String ParameterValue = responseDishListEntity.getData().getPostBackParameter().getParameterValue();
            while (1 == 1) {
                ResponseDishListEntity temp = getAllDish(shopId, parameterType, ParameterValue);
                if ("success".equals(temp.getStatus()) &&
                        StringUtils.isNotBlank(temp.getData().getPostBackParameter().getParameterType()) &&
                        StringUtils.isNotBlank(temp.getData().getPostBackParameter().getParameterValue())) {
                    //参数重新复制
                    parameterType = temp.getData().getPostBackParameter().getParameterType();
                    ParameterValue = temp.getData().getPostBackParameter().getParameterValue();

                    //复制数据
                    List<DishEntity> list = responseDishListEntity.getData().getResult();
                    list.addAll(temp.getData().getResult());
                    responseDishListEntity.getData().setResult(list);
                    continue;
                } else {
                    break;
                }
            }
        }

        return responseDishListEntity;
    }

    public ResponseDishListEntity getAllDish(long shopId, String parameterType, String parameterValue) throws Exception {
        Map paramMap = new HashMap();
        paramMap.put("appId", getAppId(shopId));
        paramMap.put("postBackParameter", new PostBackParameterEntity(parameterType, parameterValue));

        String requestBody = JsonUtil.serialize(paramMap);
        log.debug("请求body:" + requestBody);

        Map<String, String> header = getHeader(shopId, requestBody);
        log.debug("请求header:" + JsonUtil.serialize(header));

        String responseJson = OkHttpUtil.post(getBaseUrl(shopId) + "productOpenApi/queryProductPages", requestBody, header);
        log.debug("返回结果:" + responseJson);

        return (ResponseDishListEntity) JsonUtil.deserialize(responseJson, ResponseDishListEntity.class);
    }

    public void download(long shopId) {
        try {
            ResponseDishListEntity entity = getAllDish(shopId);
            if ("success".equals(entity.getStatus())) {
                List<DishEntity> list = entity.getData().getResult();
                if (list != null && list.size() > 0) {
                    for (DishEntity item : list) {
                        TDishEntity dishEntity = new TDishEntity();
                        dishEntity.setShopId(shopId);
                        dishEntity.setPospalId(String.valueOf(item.getUid()));
                        dishEntity.setBarcode(item.getBarcode());
                        dishEntity.setDishName(item.getName());

                        TDishEntity rtn = dishDao.getDish(String.valueOf(item.getUid()));
                        if (rtn != null) {
                            dishEntity.setId(rtn.getId());
                        }

                        dishDao.saveOrUpdate(dishEntity);
                    }
                }
            }
        } catch (Exception e) {
            log.error("下载PosPal菜单信息:" + e.getMessage());
            e.printStackTrace();
        }
    }
}
